package weaver.formmode.customjavacode.modeexpand.srm.cg;

import weaver.common.DateUtil;
import weaver.conn.BatchRecordSet;
import weaver.conn.RecordSet;
import weaver.formmode.customjavacode.AbstractModeExpandJavaCodeNew;
import weaver.general.BaseBean;
import weaver.general.Util;
import weaver.hrm.User;
import weaver.formmode.customjavacode.modeexpand.srm.util.SrmUtil;
import weaver.soa.workflow.request.RequestInfo;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * @Description: 物料类需求拆分新建模板的新建保存
 * @Param:
 * @return:
 * @Author: Dusongsong
 * @Date: 2021/3/23
 */
public class WeaWlxqcfNewSaveAction extends AbstractModeExpandJavaCodeNew {

    public Map<String, String> doModeExpand(Map<String, Object> param) {
        Map<String, String> result = new HashMap<String, String>();
        try {
            User user = (User) param.get( "user" );
            int billid = -1;//数据id
            int modeid = -1;//模块id
            RequestInfo requestInfo = (RequestInfo) param.get( "RequestInfo" );
            if (requestInfo != null) {
                billid = Util.getIntValue( requestInfo.getRequestid() );
                modeid = Util.getIntValue( requestInfo.getWorkflowid() );
                if (billid > 0 && modeid > 0) {
                    //------请在下面编写业务逻辑代码------
                    RecordSet rs = new RecordSet();
                    RecordSet rs1 = new RecordSet();
                    BatchRecordSet batchRecordSet = new BatchRecordSet();
                    List<List<Object>> resultList = new ArrayList<List<Object>>();
                    String cglx = "0";
                    String rwbh = SrmUtil.generTaskCode( cglx );//任务编号
                    rs.writeLog( "WeaWlxqcfNewSaveAction-----------------" + billid );
                    //插入主表
                    String comFields = "xqly,xqdjr,xqdw,sfjcxq,xqrq,xqlx,xmmc,xmbh,xmxz,wllb,\n" +
                            "ppfw,ygje,spdhrq,cgqx,sfbq,fj,xqdh,xqbh,bgdh,bgbh,\n" +
                            "jbgsl,xqfl,jcdh,htdh,jhdbjcsj,cgms,cgy,cgfs,pbfs,shdz,\n" +
                            "pid,bz,bmid,fbid,bglsh,xmxzid,xqdwid,xqdwmc,xmid";

                    String sql = "select " + comFields + ",xqdid,zcdbjcsj from uf_xqcf_wl where id=?";
                    rs.executeQuery( sql, billid );
                    if (rs.next()) {
                        String xqly = Util.null2String( rs.getString( "xqly" ) );//需求来源
                        String xqdjr = Util.null2String( rs.getString( "xqdjr" ) );//需求对接人
                        String xqdw = Util.null2String( rs.getString( "xqdw" ) );//需求单位ID
                        String sfjcxq = Util.null2String( rs.getString( "sfjcxq" ) );//是否集采需求
                        String xqrq = Util.null2String( rs.getString( "xqrq" ) );//需求日期
                        String xqlx = Util.null2String( rs.getString( "xqlx" ) );//需求类型
                        String xmmc = Util.null2String( rs.getString( "xmmc" ) );//项目名称
                        String xmbh = Util.null2String( rs.getString( "xmbh" ) );//项目编号
                        String xmxz = Util.null2String( rs.getString( "xmxz" ) );//项目性质
                        String wllb = Util.null2String( rs.getString( "wllb" ) );//物料类别
                        String ppfw = Util.null2String( rs.getString( "ppfw" ) );//品牌范围
                        String ygje = Util.null2String( rs.getString( "ygje" ) );//预估金额
                        String spdhrq = Util.null2String( rs.getString( "spdhrq" ) );//首批到货日期
                        String cgqx = Util.null2String( rs.getString( "cgqx" ) );//采购权限
                        String sfbq = Util.null2String( rs.getString( "sfbq" ) );//是否标前
                        String fj = Util.null2String( rs.getString( "fj" ) );//附件
                        String xqdh = Util.null2String( rs.getString( "xqdh" ) );//需求单号
                        String xqbh = Util.null2String( rs.getString( "xqbh" ) );//需求包号
                        String bgdh = Util.null2String( rs.getString( "bgdh" ) );//变更单号
                        String bgbh = Util.null2String( rs.getString( "bgbh" ) );//变更包号
                        String jbgsl = Util.null2String( rs.getString( "jbgsl" ) );//仅变更数量
                        String xqfl = Util.null2String( rs.getString( "xqfl" ) );//需求分类
                        String jcdh = Util.null2String( rs.getString( "jcdh" ) );//决策单号
                        String htdh = Util.null2String( rs.getString( "htdh" ) );//合同单号
                        String jhdbjcsj = Util.null2String( rs.getString( "jhdbjcsj" ) );//计划定标决策时间
                        String cgms = Util.null2String( rs.getString( "cgms" ) );//采购模式
                        String cgy = Util.null2String( rs.getString( "cgy" ) );//采购员
                        String cgfs = Util.null2String( rs.getString( "cgfs" ) );//采购方式
                        String pbfs = Util.null2String( rs.getString( "pbfs" ) );//评标方式
                        String shdz = Util.null2String( rs.getString( "shdz" ) );//收货地址
                        String pid = Util.null2String( rs.getString( "pid" ) );//PID
                        String bz = Util.null2String( rs.getString( "bz" ) );//备注
                        String bmid = Util.null2String( rs.getString( "bmid" ) );//部门ID
                        String fbid = Util.null2String( rs.getString( "fbid" ) );//分部id
                        String bglsh = Util.null2String( rs.getString( "bglsh" ) );//变更流水号
                        String xmxzid = Util.null2String( rs.getString( "xmxzid" ) );//项目性质ID
                        String xqdwid = Util.null2String( rs.getString( "xqdwid" ) );//需求单位ID-树
                        String xqdwmc = Util.null2String( rs.getString( "xqdwmc" ) );//需求单位名称
                        String xmid = Util.null2String( rs.getString( "xmid" ) );//项目ID
                        String xqdid = Util.null2String( rs.getString( "xqdid" ) );//需求单位ID
                        String zcdbjcsj = Util.null2String( rs.getString( "zcdbjcsj" ) );//最迟定标决策时间
                        String nd = xqrq.substring( 0, 4 );//年度
                        String rwzt = "0";//任务状态
                        String cfdid = Util.null2String(billid);
                        String formmodeid = SrmUtil.getFormmodeId( cglx );
                        String modedatacreatertype = "0";
                        String modedatacreater = user.getUID() + "";
                        String modedatacreatedate = DateUtil.getCurrentDate();
                        String fullDate = DateUtil.getFullDate();
                        String modedatacreatetime = fullDate.substring( 11, 19 );
                        String fields = comFields + ",rwzt,xqdid,rwbh,zcdbjcsj,cfdid,nd,cglx," +
                                "formmodeid,modedatacreater,modedatacreatertype,modedatacreatedate,modedatacreatetime";
                        String values = "values(" +
                                "?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?," +
                                "?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?," +
                                "?,?,?,?,?,?,?,?,?,?,?)";
                        String sql1 = "insert into uf_cgrw_wl (" + fields + ") " + values;
                        rs.writeLog( sql1 );
                        Object[] params = {xqly, xqdjr, xqdw, sfjcxq, xqrq, xqlx, xmmc, xmbh, xmxz, wllb,
                                ppfw, ygje, spdhrq, cgqx, sfbq, fj, xqdh, xqbh, bgdh, bgbh,
                                jbgsl, xqfl, jcdh, htdh, jhdbjcsj, cgms, cgy, cgfs, pbfs, shdz,
                                pid, bz, bmid, fbid, bglsh, xmxzid, xqdwid, xqdwmc, xmid, rwzt,
                                xqdid, rwbh, zcdbjcsj, cfdid, nd, cglx,
                                formmodeid, modedatacreater, modedatacreatertype, modedatacreatedate, modedatacreatetime};
                        if (rs.executeUpdate( sql1, params )) {
                            rs.writeLog( "-----------------插入明细表---------------" );
                            String mainid = SrmUtil.getRwbh2Id( rwbh, cglx );//id
                            SrmUtil.getNewModeRight( cgy, formmodeid, mainid );
                            //插入明细表
                            String sql2 = "select wm_concat(id) as id,wm_concat(xqmxid) as xqmxid,wlbm,wlmc,cms,xlh,xlbz," +
                                    "dw,sum(sl) as sl,fzdw,sum(fzsl) as fzsl,sum(cgsl) as cgsl,sum(sysl) as sysl,fbbh,zfbbh," +
                                    "zfbid,wlid from uf_xqcf_wl_dt1 where mainid=? and gxcf=1\n" +
                                    "group by wlbm,wlmc,cms,xlh,xlbz,dw,fzdw,fbbh,zfbbh,zfbid,wlid";
                            rs1.executeQuery( sql2, billid );
                            while (rs1.next()) {
                                List<Object> list = new ArrayList<>();
                                String wlbm = Util.null2String( rs1.getString( "wlbm" ) );//物料编码
                                String wlmc = Util.null2String( rs1.getString( "wlmc" ) );//物料名称
                                String cms = Util.null2String( rs1.getString( "cms" ) );//长描述
                                String xlh = Util.null2String( rs1.getString( "xlh" ) );//序列号
                                String xlbz = Util.null2String( rs1.getString( "xlbz" ) );//序列备注
                                String dw = Util.null2String( rs1.getString( "dw" ) );//单位
                                String sl = Util.null2String( rs1.getString( "sl" ) );//数量
                                String fzdw = Util.null2String( rs1.getString( "fzdw" ) );//辅助单位
                                String fzsl = Util.null2String( rs1.getString( "fzsl" ) );//辅助数量
                                String cgsl = Util.null2String( rs1.getString( "cgsl" ) );//采购数量
                                String sysl = Util.null2String( rs1.getString( "sysl" ) );//剩余数量
                                String fbbh = Util.null2String( rs1.getString( "fbbh" ) );//分部编号
                                String zfbbh = Util.null2String( rs1.getString( "zfbbh" ) );//子分部编号
                                String zfbid = Util.null2String( rs1.getString( "zfbid" ) );//子分部id
                                String wlid = Util.null2String( rs1.getString( "wlid" ) );//物料id
                                String xqmxid = Util.null2String( rs1.getString( "xqmxid" ) );//需求明细id
                                list.add( mainid );
                                list.add( wlbm );
                                list.add( wlmc );
                                list.add( cms );
                                list.add( xlh );
                                list.add( xlbz );
                                list.add( dw );
                                list.add( sl );
                                list.add( fzdw );
                                list.add( fzsl );
                                list.add( cgsl );
                                list.add( sysl );
                                list.add( fbbh );
                                list.add( zfbbh );
                                list.add( zfbid );
                                list.add( wlid );
                                list.add( xqmxid );
                                resultList.add( list );
                                //更新物料采购需求池的勾选拆分
                                rs1.executeUpdate( "update uf_cgxq_wl_dt1 set cfbj=1 where id in (" + xqmxid + ")" );
                            }

                            String sql3 = "insert into uf_cgrw_wl_dt1(mainid,wlbm,wlmc,cms," +
                                    "xlh,xlbz,dw,sl,fzdw, fzsl,cgsl,sysl,fbbh,zfbbh,zfbid,wlid,xqmxid) " +
                                    "values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
                            batchRecordSet.executeBatchSql( sql3, resultList );
                            rs.writeLog( "sql3="+sql3 );
                            if (SrmUtil.getDtlCounts( xqdid, cglx )) {
                                rs1.executeUpdate( "update uf_cgxq_wl set fplx=1 where id in (" + xqdid + ")" );
                            }
                        }
                    }
                }
            }
        } catch (Exception e) {
            result.put( "errmsg", "自定义出错信息" );
            result.put( "flag", "false" );
        }
        return result;
    }


}